<?php
# 文件名称:fdform.model.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class fdformModel extends C_model {
	var $_lists   = array();
	
	var $_pagination = null;
	
	var $_namefield = null;
	
    function fdformModel()
    {
        parent::C_model();
    }
	//表单的主题风格
	function getThemes()
	{
		$path = PATH_SITE.DS.'templates'.DS.'forms';
		//目录	
		$fso    =   opendir($path);
		$result = 	array();
		if($fso)
		{
			while($file=readdir($fso))
			{
				if($file!='.' && $file!='..')
				{			
					$filepath=$path.DS.$file;
					//处理目录
					if(is_dir($filepath)) 
					{											
						$result[] = C_html::_('select.option',  $file, $file);						
					}					
				}				
			}
		}
		return $result;
	}
	//具体单个表单
	function getItem()
	{
		if (!empty( $this->_one )) 
		{
			return $this->_one;
		}
		$SYS =&get_instance();
		//参数过滤
		$id = $SYS->request->getVar( 'cid', array(0), '', 'array' );
		//数据
		$query = 'SELECT *' .
				' FROM '.$this->_db->_table_prefix.'fdform'.
				' WHERE id='.$id[0]
				;
		$this->_db->setQuery($query);
		$this->_one = $this->_db->loadAssoc();
		//表单名
		$this->_namefield 		= $this->getFieldName($id[0],"name");
		//回复内容
		$this->_replycontent 	= $this->getFieldName($id[0],"replycontent");
		//介绍
		$this->_introducefield  = $this->getFieldName($id[0],"introduce");
		//回复主题
		$this->_replysubject 	= $this->getFieldName($id[0],"replysubject");
		
		//邮件字段
		$query = 'SELECT a.id,b.fieldvalue name' .
				' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				' ,'.$this->_db->_table_prefix.'language'.' AS c '.
				' WHERE a.use_ok=1 '.
				' AND a.form='.$id[0].
				' AND b.recordid=a.id'.
				' AND b.module=3'.
				' AND b.lang=c.id'.
				' AND b.fieldname="name"'.
				' AND c.backend=1'
				;
		$this->_db->setQuery($query);
		$field[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' -', 'id', 'name');
		$field = array_merge($field, $this->_db->loadObjectList());
		$this->_mail = C_html::_('select.genericlist',  $field, 'replymailto', 'class="inputbox" size="1" ', 'id', 'name', $this->_one['replymailto']);
		
		return $this->_one;
	}
	//相应语言字段
	function getFieldName($id,$name)
	{
		//语言
		$query = 'SELECT b.id,b.fieldvalue,c.`default`,c.id langid'.
				' FROM '.$this->_db->_table_prefix.'fields'.' AS b '.
				' ,'.$this->_db->_table_prefix.'language'.' AS c '.
				' WHERE b.recordid='.$id.
				' AND b.module=1'.
				' AND b.lang=c.id'.
				' AND b.fieldname="'.$name.'"'.
				' ORDER BY c.`backend` desc,langid'				
		;
		
		$this->_db->setQuery($query);
		return $this->_db->loadAssocList();
	}
	//表单名
	function getName()
	{
		return $this->_namefield;
	}
	//回复内容
	function getReplyContent()
	{
		return $this->_replycontent;
	}
	//介绍
	function getIntroduce()
	{
		return $this->_introducefield;
	}
	//回复主题
	function getReplySubject()
	{
		return $this->_replysubject;
	}
	//表单数据集
	function getItems()
	{
		if (!empty( $this->_data ))
		{
			return $this->_data;
		}
		$SYS =&get_instance();
		$currentUser = & $SYS->session->get('admin_name');
		// 获取请求的数据
		$filter_order		= $SYS->request->getVar( "filter_order",		'a.modified_date',		'post',	'cmd' );
		$filter_order_Dir	= $SYS->request->getVar( "filter_order_Dir",	'desc',	'post',			'word' );
		$search				= $SYS->request->getVar( "search",			'', 			'post',			'string' );
		$limit = $SYS->request->getInt( 'limit',10 );
		$limitstart = $SYS->request->getInt( 'limitstart' );
		//过滤查询
		$where = array();
		if (isset( $search ) && $search!= '')
		{
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
			$tmp = '(b.fieldvalue LIKE '.$searchEscaped;
			//管理员可以搜索创建者
			if(in_array("1",explode(",",$currentUser->get('gid'))))
			{
				$query = 'SELECT id FROM '.$this->_db->_table_prefix.'admin_table WHERE admin_id LIKE'.$searchEscaped;
				$this->_db->setQuery($query);
				$tmpid=$this->_db->loadResultArray();
				$tmpid=implode(',',$tmpid);
				$tmp .= ' OR a.created_by IN ('.$tmpid.')';
			}
			$tmp .= ')';
			$where[] = $tmp;
		}
		//语言列
		$where[]='a.id=b.recordid';
		$where[]='b.module=1';
		$where[]='b.fieldname="name"';
		$defaultlang=$SYS->lang->getDefault(1,'backend');
		$where[]='b.lang='.$defaultlang['id'];
		//权限
		if(!in_array("1",explode(",",$currentUser->get('gid'))))
		{
			$where[] = 'a.created_by='.$currentUser->get('id');
		}
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');
		//分页
		_import('html/pagination',false);
		$query = 'SELECT COUNT(*)' .
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				$where.
				$orderby;
		$this->_db->setQuery($query);
		$total = $this->_db->loadResult();
		$this->_pagination = new C_pagination( $total, $limitstart, $limit );
		//数据集
		$query = 'SELECT a.no_order,b.fieldvalue,a.created_by,a.enabled,a.modified_date,a.id' .
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				$where.
				$orderby;
		
		$this->_data = $this->_getArrayList( $query, $this->_pagination->limitstart, $this->_pagination->limit );
		//排序
		$this->_lists['order_Dir']	= $filter_order_Dir;
		$this->_lists['order']		= $filter_order;
		$this->_lists['search']		= $search;
		return $this->_data;
	}
	function getLists()
	{		
		return $this->_lists;
	}
	//语言字段
	function getEmailField()
	{
		return $this->_mail;		
	}
	//分页
	function getPagination()
	{
		return $this->_pagination;
	}
	
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>